package com.ocean.web.config;

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

/**
 * @Author: FuGui Wang
 * @Description: 主要配置多视图实现的视图解析器相关bean实例
 *
 *  其实关键点在于两个：
 *  1、配置order属性
 *  2、配置viewnames属性
 *  注意：
 *  return new ModelAndView("jsp/index");//或者return "jsp/index"
 *  对应 /WEB-INF/jsp/index.jsp
 *  同理：
 *  return "thymeleaf/index";//或者return “thymeleaf/index”
 *  对应 /WEB-INF/thymeleaf/index.html
 *
 * @Date 2022/12/8
 * @Version V1.1.0
 */
@Configuration
public class ViewResolverConfigurationConfig {
    private  final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/"};

    /**
     * 用来定义 DispatcherServlet 应用上下文中的 bean
     */
    @Configuration
    @EnableWebMvc
    @ComponentScan("com.ocean.web.controller")
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Bean
        public ThymeleafViewResolver viewResolverThymeLeaf() {
            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            viewResolver.setCharacterEncoding("utf-8");
            viewResolver.setOrder(1);
            //viewResolver.setViewNames(new String[]{"thyme/*"});
            viewResolver.setViewNames(new String[]{"thymeleaf/*","vue/*"});
            return viewResolver;
        }

        /**
         * jsp目录
         */
        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/");
            resolver.setSuffix(".jsp");
            resolver.setViewNames("jsp/*");
            resolver.setOrder(2);
            return resolver;
        }

        /**
         * html目录
         */
        @Bean
        public ITemplateResolver templateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setTemplateMode("HTML");
            templateResolver.setPrefix("classpath:/templates/");
            templateResolver.setSuffix(".html");
            templateResolver.setCharacterEncoding("utf-8");
            templateResolver.setCacheable(false);
            return templateResolver;
        }

        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver());
            // templateEngine
            return templateEngine;
        }

        @Bean
        WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> enableDefaultServlet() {
            return (factory) -> factory.setRegisterDefaultServlet(true);
        }

        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }

        /**
         * 配置静态资源
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            /*if (!registry.hasMappingForPattern("/plug-in/**")) {
                registry.addResourceHandler("/plug-in/**").addResourceLocations(
                        "classpath:/META-INF/webapp/plug-in/");
            }*/
            registry.addResourceHandler("/data/**").addResourceLocations("file:/opt/data");//映射L级产品下载路径
            if (!registry.hasMappingForPattern("/**")) {
                registry.addResourceHandler("/**")
                        .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
                registry.addResourceHandler("/static/**").addResourceLocations("static/");
                registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            }
        }
    }
}
